<?php

namespace app\api\controller\wm;


use app\common\controller\Api;
use app\common\model\wm\Cart as WashCart;

/**
 * 外卖购物车
 * @ApiWeigh (95)
 * 
 */
class Cart extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // protected $auth = ['test3'];

    /**
     * 购物车列表
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="admin_id", type="int", required=true, description="门店ID")
     * @ApiParams (name="limit", type="int", required=true, description="每页数量")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':'0','msg':'返回成功'})
     */
    public function list()
    {
        $user = $this->auth->getUser();
        $rej = $this->validate(
            $this->request->post(),
            [
                'admin_id' => 'require|gt:0'
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $res = WashCart::with(['goods'])->where('cart.admin_id', $this->request->post('admin_id'))->where('user_id', $user->id)->paginate($this->request->post('limit'))->toArray();

        $this->success('', $res);
    }


    /**
     * 添加购物车
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="商品ID")
     * @ApiParams (name="num", type="integer", required=true, description="数量")
     * @ApiParams (name="admin_id", type="int", required=true, description="门店ID")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function add()
    {
        $user = $this->auth->getUser();
        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|gt:0',
                'num' => 'require|number|min:1|max:99',
                'admin_id' => 'require|gt:0'
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }

        $res = WashCart::where('user_id', $user->id)->where('goods_id', $this->request->post('id'))->where('admin_id', $this->request->post('admin_id'))->find();
        if ($res) {
            $res->total += $this->request->post('num');
            $res->save();
            $this->responseMsg(0);
        }
        $data = [
            'user_id' => $user->id,
            'goods_id' => $this->request->post('id'),
            'total' => $this->request->post('num'),
            'admin_id' => $this->request->post('admin_id'),

        ];
        $res = new WashCart($data);
        $res->save();
        $this->responseMsg(0);
    }

    /**
     * 更新数量
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="商品ID")
     * @ApiParams (name="num", type="integer", required=true, description="数量")
     * @ApiParams (name="admin_id", type="int", required=true, description="门店ID")
     * 
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function update()
    {
        $user = $this->auth->getUser();
        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number',
                'num' => 'require|number|min:1|max:99',
                'admin_id' => 'require|gt:0'

            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }

        $res = WashCart::where('user_id', $user->id)->where('admin_id', $this->request->post('admin_id'))->where('goods_id', $this->request->post('id'))->find();
        if (!$res) {
            $this->responseMsg(1007, '', '没有找到数据');
        }
        $res->total = $this->request->post('num');
        $res->save();
        $this->responseMsg(0);
    }






    /**
     * 删除购物车
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="ids", type="integer|array", required=true, description="商品ids")
     * @ApiParams (name="admin_id", type="int", required=true, description="门店ID")
     * 
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function del()
    {

        $rej = $this->validate(
            $this->request->post(),
            [
                'ids'  => 'require',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        // $data = $this->request->put();
        // if(empty($data['ids'])){
        //     $this->responseMsg(1005, [], 'ids不能为空');
        // }
        // $ids = $data['ids'];
        // $arr = [];
        // foreach($ids as $id){
        //     array_push($arr,intval($id));
        // }

        $res = WashCart::where('id', 'in', explode(',', $this->request->post('ids')))->delete();
        if (!$res) {
            $this->responseMsg(1007, '', '没有找到数据');
        }
        $this->responseMsg(0);
    }
}
